//
//  AMBlurView.m
//  blur
//
//  Created by Cesar Pinto Castillo on 7/1/13.
//  Copyright (c) 2013 Arctic Minds Inc. All rights reserved.
//

#import "AMBlurView.h"
#import <QuartzCore/QuartzCore.h>
#import "UIColor+Help.h"
@interface AMBlurView ()

@property (nonatomic, strong) UIToolbar *toolbar;
@property (nonatomic, strong) UIVisualEffectView *visualEffectView;

@end

@implementation AMBlurView

- (instancetype)initWithCoder:(NSCoder *)aDecoder {
    self = [super initWithCoder:aDecoder];
    if (self) {
        [self setup];
    }
    return self;
}

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self setup];
    }
    return self;
}

- (instancetype)init
{
    self = [super init];
    if (self) {
        [self setup];
    }
    return self;
}

- (void)setup {
    if (NSClassFromString(@"UIVisualEffectView")) {
        UIVisualEffect *blurEffect;
        blurEffect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleLight];
        
        if (NSClassFromString(@"UIVisualEffectView")) {
            self.visualEffectView = [[UIVisualEffectView alloc] initWithEffect:blurEffect];
            [self.layer insertSublayer:[self.visualEffectView layer] atIndex:0];
            self.visualEffectView.userInteractionEnabled = NO;
        }
//        self.backgroundColor = [UIColor colorWithHex:0x000000 alpha:0.6];
    } else {
        // If we don't clip to bounds the toolbar draws a thin shadow on top
        [self setClipsToBounds:YES];
        if (![self toolbar]) {
            [self setToolbar:[[UIToolbar alloc] initWithFrame:[self bounds]]];
            [self.layer insertSublayer:[self.toolbar layer] atIndex:0];
            self.toolbar.barStyle = UIBarStyleBlack;
        }
    }
}

- (void)setBgColor:(UIColor *)bgColor
{
    if (NSClassFromString(@"UIVisualEffectView")) {
        self.backgroundColor = bgColor;
    }
}

- (void)layoutSubviews {
    [super layoutSubviews];
    self.visualEffectView.frame = self.bounds;
    [self.toolbar setFrame:[self bounds]];
}

@end
